﻿using Simple.Admin.System;
using Simple.Admin.System.Enums;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Uow;

namespace Simple.Admin.Data;

public class MenuEntityDataSeedContributor : IDataSeedContributor, ITransientDependency
{

    private readonly IRepository<PermissionMenuEntity> _permissionMenuRepository;

    private readonly IIdentityRoleRepository _identityRoleRepository;

    private readonly IMenuEntityRepository _menuRepository;
    public MenuEntityDataSeedContributor(IRepository<PermissionMenuEntity> permissionMenuRepository, IIdentityRoleRepository identityRoleRepository, IMenuEntityRepository menuRepository)
    {
        _permissionMenuRepository = permissionMenuRepository;
        _identityRoleRepository = identityRoleRepository;
        _menuRepository = menuRepository;
    }

    [UnitOfWork]
    public async Task SeedAsync(DataSeedContext context)
    {
        if(await _menuRepository.AnyAsync())
        {
            return;
        }
        var systemId = Guid.NewGuid();

        var menus = new List<MenuEntity>
        {
            // Level 1 - Dashboard
            new MenuEntity(Guid.NewGuid())
            {
                Name = "Dashboard",
                Title = "控制台",
                Path = "/dashboard",
                Icon = "dashboard",
                Type = MenuTypeEnum.Menu,
                Order = 1,
                KeepAlive = true
            },

            // Level 1 - System Management (Parent)
            new MenuEntity(systemId)
            {
                Name = "System",
                Title = "系统管理",
                Path = "/system",
                Icon = "setting",
                Type = MenuTypeEnum.Directory,
                Order = 2,
                HideChildrenInMenu = false,
                Redirect = "/system/user"
            },

            // Level 2 - User Management
            new MenuEntity(Guid.NewGuid())
            {
                Name = "User",
                Title = "用户管理",
                Path = "/system/user",
                ParentId = systemId,
                Icon = "user",
                Type = MenuTypeEnum.Menu,
                Order = 1,
            },

            // Level 2 - Role Management
            new MenuEntity(Guid.NewGuid())
            {
                Name = "Role",
                Title = "角色管理",
                Path = "/system/role",
                ParentId = systemId,
                Icon = "team",
                Type = MenuTypeEnum.Menu,
                Order = 2,
            },
            new MenuEntity(Guid.NewGuid())
            {
                Name = "Organization",
                Title = "组织管理",
                Path = "/system/oeganization",
                ParentId=systemId,
                Icon = "link",
                Type = MenuTypeEnum.Menu,
                Order = 3,
            },
          
        };

        var adminRole=await _identityRoleRepository.FindByNormalizedNameAsync("ADMIN");

        var permissionMenuList = new List<PermissionMenuEntity>();

        foreach (var item in menus)
        {
            permissionMenuList.Add(new PermissionMenuEntity()
            {
                ProviderType=PermissionProviderType.Role,
                ProviderKey=adminRole.Id.ToString(),
                MenuId=item.Id,
            });
        }

            
       await _menuRepository.InsertManyAsync(menus);

       await _permissionMenuRepository.InsertManyAsync(permissionMenuList);


    }
}
